{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bbfcf7c2",
   "metadata": {},
   "source": [
    "# 字典的方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36007469",
   "metadata": {},
   "source": [
    "## 字典的查询\n",
    ">1.in方法的查询  \n",
    ">2.get方法的获取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "87eb4304",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1801': 'lihaha', 'stu1802': 'kaijiayongshi', 'stu1803': 'guosiwen'}\n"
     ]
    }
   ],
   "source": [
    "info = {\n",
    "    \"stu1801\":\"lihaha\",\n",
    "    \"stu1802\":\"kaijiayongshi\",\n",
    "    \"stu1803\":\"guosiwen\"\n",
    "}\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0e1d33e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询 key 是否在字典当中 用 in \n",
    "\"stu1801\" in info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4259899c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询 key在不在的结果为True or False\n",
    "\"stu1807\" in info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "444b8e32",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'lihaha'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# get 方法 获取字典中的值（ key 对应的value内容）\n",
    "info.get(\"stu1801\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "464b6983",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lihaha\n"
     ]
    }
   ],
   "source": [
    "# 综上所述：字典的查询方法一般步骤如下：\n",
    "if \"stu1801\" in info:\n",
    "    print(info.get(\"stu1801\"))\n",
    "else:\n",
    "    print(\"您查询的内容不存在\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dbe05e94",
   "metadata": {},
   "source": [
    "## 字典的删除\n",
    ">1.pop()指定删除值  \n",
    ">2.popitem() 随机删除  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "5612a668",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'lihaha', 'stu1802': 'kaijiayongshi', 'stu1803': 'guosiwen'}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info   # 获取所有信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "8abe7290",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'lihaha'"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info.pop(\"stu1801\") # 指定删除1801编号的学生"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "94e8e095",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1802': 'kaijiayongshi', 'stu1803': 'guosiwen'}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info  # 再次获取信息查看结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "f2330770",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('stu1803', 'guosiwen')"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info.popitem()   # 随机删除一个信息，每一次运行结果都不一样maybe可以用于抽签？？哈哈哈哈"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2184d5eb",
   "metadata": {},
   "source": [
    "# 字典的一般方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a55066d",
   "metadata": {},
   "source": [
    "## 字典的循环遍历\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "0278bf2f",
   "metadata": {},
   "outputs": [],
   "source": [
    "info = {\n",
    "    \"stu1801\":\"ZiQi\",\n",
    "    \"stu1802\":\"XiaoFeng\",\n",
    "    \"stu1803\":\"LiuYu\"\n",
    "}     # 写下字典库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "331c6108",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'ZiQi', 'stu1802': 'XiaoFeng', 'stu1803': 'LiuYu'}"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info   # 输出字典库信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "f1132594",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1801 ZiQi\n",
      "1802 XiaoFeng\n",
      "1803 LiuYu\n"
     ]
    }
   ],
   "source": [
    "# 循环遍历字典\n",
    "# 循环遍历的最大用途就是：给所有的值使用同样的 操作/方法\n",
    "stu_id_list = []\n",
    "for k,v in info.items():\n",
    "    print(k[3:],v)\n",
    "    stu_id_list.append(k[3:])\n",
    "    \n",
    "#     即是给所有的值都定下一个输出格式：键值对为(k[3],v),从key的序号[3]输出   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "20bd823e",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['1801', '1802', '1803']"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_id_list   # 输出学生编号"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e46058d",
   "metadata": {},
   "source": [
    "### 课后练习\n",
    ">请大家把info的 value 目前的格式进行统一修改，修改的范式为： 姓 名 Zi Qi Xiao Feng"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6b966ef",
   "metadata": {},
   "source": [
    "## fromkeys()方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "cb21ede5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function fromkeys:\n",
      "\n",
      "fromkeys(iterable, value=None, /) method of builtins.type instance\n",
      "    Create a new dictionary with keys from iterable and values set to value.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(info.fromkeys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "54cdd4e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stud2101': 123456, 'stud2102': 123456, 'stud2103': 123456}"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 初始化统一值的字典信息\n",
    "学生初始密码={}\n",
    "学生初始密码.fromkeys([\"stud2101\",\"stud2102\",\"stud2103\"],123456)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e2bb465",
   "metadata": {},
   "source": [
    "## setdefault()方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "fbeb6e9e",
   "metadata": {},
   "outputs": [],
   "source": [
    "info.setdefault('stu1804')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "e1ebc5e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'zhichao'"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info.setdefault('stu1805','zhichao')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "37c806dc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'ZiQi',\n",
       " 'stu1802': 'XiaoFeng',\n",
       " 'stu1803': 'LiuYu',\n",
       " 'stu1804': None,\n",
       " 'stu1805': 'zhichao'}"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e11b4c25",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
